﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using SKYPE4COMLib;
using System.Threading;
using System.Windows.Threading;

namespace Gype.GUI
{
    public class Manager
    {
        public Manager(SlimDX.Direct3D9.Device dev, Skype skype)
        {
            SetSkype(skype);
            initFromTarget(UI.Rendertarget.CreateTarget(dev));
            mGraphicsDispatcher = Dispatcher.CurrentDispatcher;

            foreach (User usr in skype.Friends)
            {
                UserManager.AddUser(usr);
            }
        }

        public Manager(SlimDX.Direct3D11.Device dev)
        {
        }

        public Manager(SlimDX.Direct3D10.Device dev)
        {
        }

        public void attachInput(System.Windows.Forms.Control ctrl)
        {
            attachInput(ctrl.Handle);
        }

        public void attachInput(IntPtr control)
        {
            if (InputSystem != null)
            {
                InputSystem.detach();
                InputSystem.attach(control);
            }
            else
            {
                InputSystem = new InputSystem(control);
                InputSystem.OnInputMessage += inputMessageCallback;
            }
        }

        public void OnFrame()
        {
            mTarget.BeginUI();

            foreach (var comp in mComponents)
                comp.RenderObject();

            mTarget.EndUI();
        }

        internal void AddComponent(Component component)
        {
            component.AttachedToManager(this);
            mComponents.Add(component);         
        }

        private void inputMessageCallback(InputMessage message)
        {
            Components.ControlMessage msg = Components.ControlMessage.Parse(message);
            for (int i = mComponents.Count - 1; i >= 0; --i)
            {
                msg.ForwardMessage(null, mComponents[i]);
            }
        }

        private void initFromTarget(UI.Rendertarget target)
        {
            mTarget = target;
            Components.TextCollection.InitFonts(target);
            Components.BrushCollection.InitBrushes(target);
            ImageCollection.Load(target);

            initMainFrame();
        }

        private void initMainFrame()
        {
            MainFrame = new Components.MainFrame();
            MainFrame.Size = new System.Drawing.SizeF(600, 500);
            MainFrame.Location = new System.Drawing.PointF(30, 50);

            AddComponent(MainFrame);
        }

        public void SetSkype(Skype skype)
        {
            if (skype == null)
                throw new ArgumentNullException();

            SkypeInterface = skype;
        }

        public Components.MainFrame MainFrame { get; private set; }

        internal Skype SkypeInterface { get; private set; }
        internal System.Windows.Threading.Dispatcher Dispatcher { get { return mGraphicsDispatcher; } }

        public UI.Rendertarget Target { get { return mTarget; } }
        public InputSystem InputSystem { get; private set; }

        private UI.Rendertarget mTarget;
        private List<Component> mComponents = new List<Component>();
        private System.Windows.Threading.Dispatcher mGraphicsDispatcher;
    }
}
